﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Options Dialog
*/

/***********************************************************************
	On Open
***********************************************************************/
function options_onOpen(){
	
	optionsFeeds =  restoreFeeds();

	if( optionsFeeds.length == 0 )
	{
		lblNoFeeds.visible = true;
		lblURL.visible = false;
		lblRemove.visible = false;
	}
	else
	{
		lblNoFeeds.visible = false;

		entry = "<listbox height=\"120\" name=\"lblists\" width=\"364\" x=\"2\" y=\"90\" background=\"#FFFFFF\" itemWidth=\"364\" itemHeight=\"20\"" 
		entry = entry + " itemOverColor=\"#30FFFFFF\" autoscroll=\"true\" itemSelectedColor=\"#30FFFFFF\"/>"
		divmain.AppendElement( entry );
		
		lblists.RemoveAllElements();

		for(var i = 0;i< optionsFeeds.length;i++)
		{
				var feed = optionsFeeds[i];
				entry = "<item height=\"20\" width=\"348\" name=\"" + i+ "\">" 
				entry = entry + "<label x=\"0\" y=\"1\" height=\"18\" width=\"250\" name=\"li" + i + "\" color=\"#000000\" font=\"tahoma\" size=\"8\">" + xmlEscape( feed.url )+ "</label>";
				entry = entry + "<button height=\"18\" name=\"button" + i +"\" width=\"75\" x=\"250\" y=\"0\" downImage=\"stock_images\\button_down.png\" font=\"Tahoma\" size=\"8\"";
				entry = entry + " image=\"stock_images\\button_up.png\" overImage=\"stock_images\\button_over.png\" caption=\"&BUTTON_EDIT;\" onclick=\"editFeed(" + i + ")\"/>";
				entry = entry +  "<checkbox height=\"20\" name=\"cbx" + i + "\" width=\"16\" x=\"330\" y=\"0\" downImage=\"stock_images\\checkbox_down.png\" value=\"0\" ";
        entry = entry + "image=\"stock_images\\checkbox_up.png\" overImage=\"stock_images\\checkbox_over.png\" caption=\"\" checkedDownImage=\"stock_images\\checkbox_checked_down.png\" ";
        entry = entry + "checkedImage=\"stock_images\\checkbox_checked_up.png\" checkedOverImage=\"stock_images\\checkbox_checked_over.png\" onclick=\"switchRemove(" + i + ")\"/>";
				entry = entry + "</item>";
				lblists.AppendElement(entry);
			}
	}
	var mins = options.getValue( "refreshMinutes" );
	var selected = 0
	switch(mins){					/* find would be nicer */
		case "10": selected = 0;
				break;
		case "20": selected = 1;
				break;
		case "30": selected = 2;
				break;
		case "60": selected = 3;
				break;
		case "120": selected = 4;
				break;
		case "240": selected = 5;
				break;
		case "300": selected = 6;
				break;
	}
	comboboxRefresh.value = mins;
	comboboxRefresh.selectedIndex = selected;
	markread.value = (options.getValue( "markread" ) == "1" );
	opacity.value = options.getValue( "opacity" );
	maxitems.value = options.getValue("maxitems");
}

/***********************************************************************
	On Cancel
***********************************************************************/
function optionsOnCancel(){
	options.putValue("url", '');
	var closeConfigureView = detailsViewData.getValue("closeConfigureView");
  closeConfigureView();  
}

/***********************************************************************
	On OK
***********************************************************************/
function optionsOnOk(){
	options.putValue("url", '');
	storeFeeds();
	options.putValue( "refreshMinutes", comboboxRefresh.value );
	if(markread.value)
		options.putValue( "markread", "1" )
	else
		options.putValue( "markread", "0" )
	options.putValue( "opacity", opacity.value );
	options.putValue( "maxitems", maxitems.value );
	var closeConfigureView = detailsViewData.getValue("closeConfigureView");
  closeConfigureView();  
}

/***********************************************************************
	Storing feeds
***********************************************************************/
function storeFeeds(){
	var tempFeed = new Array();

	var feed;
	for(var i = 0;i< optionsFeeds.length;i++)
	{
		if( ! optionsFeeds[i].remove )
		{
			feed = new Object
			feed.url = optionsFeeds[i].url;
			feed.user = optionsFeeds[i].user;
			feed.password = optionsFeeds[i].password;
			feed.login = optionsFeeds[i].login;
			feed.lastread = optionsFeeds[i].lastread;
			tempFeed.push( feed )
		}
	}
	saveFeeds( tempFeed );
}

/***********************************************************************
	Checkbox for Removing
***********************************************************************/
function switchRemove( i ){
	
	if( optionsFeeds[i].remove )
		optionsFeeds[i].remove = false;
	else
		optionsFeeds[i].remove = true;

}

/***********************************************************************
	Edit Feed
***********************************************************************/
function editFeed( i ){
	var 	url = optionsFeeds[i].url;

	options.putValue("url", url);
	var closeConfigureView = detailsViewData.getValue("closeConfigureView");
  closeConfigureView();  

}